package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.VipGradeItem;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.VipProjectBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter.HomeVipPageAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.webview.WebActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/10.
 * Desc   :
 */

public class MyVipActivity extends BaseActivity {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.tv_vip_level)
    TextView tvVipLevel;
    @BindView(R.id.ll_vip_level)
    LinearLayout llVipLevel;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    @BindView(R.id.ll_vip_all)
    LinearLayout llVipAll;
    @BindView(R.id.iv_vip_logo)
    ImageView ivVipLogo;
    @BindView(R.id.tv_vip_content)
    TextView tvVipContent;
    @BindView(R.id.scroll_vip_level)
    ScrollView scrollVipLevel;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_my_vip);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        String token = SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getToken();
        imgLeft.setImageResource(R.mipmap.icon_close_black);
        txtTitle.setText("会员");
        txtRight.setText("申请成为发起人");
        txtRight.setTextSize(12);
        RetrofitSource.createApi(HttpPostService.class).user_vip(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<VipProjectBean>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<VipProjectBean> response) {
                        VipProjectBean data = response.getData();
                        if (data.getName() == null) {
                            llVipAll.setVisibility(View.VISIBLE);
                            scrollVipLevel.setVisibility(View.GONE);
                            getAllVipInfo();
                        } else {
                            llVipAll.setVisibility(View.GONE);
                            scrollVipLevel.setVisibility(View.VISIBLE);
                            GlideImgManager.glideLoader(MyVipActivity.this, RetrofitSource.image_url + data.getPic(),
                                    R.mipmap.img_error, R.mipmap.img_error, ivVipLogo);
                            tvVipLevel.setText(String.format(ResourcesUtils.getString(R.string.vip_level), data.getName()));
                            tvVipContent.setText(data.getGrade().get(0).getTitle());
                        }
                    }
                });
    }

    private void getAllVipInfo() {
        RetrofitSource.createApi(HttpPostService.class).grade()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<VipGradeItem>>>(this) {
                    @Override
                    public void onSuccess(ResponseBase<List<VipGradeItem>> response) {
                        HomeVipPageAdapter pageAdapter = new HomeVipPageAdapter(MyVipActivity.this, response.getData());
                        viewpager.setAdapter(pageAdapter);
                    }
                });
    }


    @OnClick({R.id.ll_leftview, R.id.ll_rightview, R.id.btn_recharge})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_rightview:
                startActivity(WebActivity.createIntent(this, "申请成为发起人", "http://gxcm.quan-oo.com/index/users/initi", null));
                break;
            case R.id.btn_recharge:
                Intent intent = new Intent(this, RechargeActivity.class);
                startActivity(intent);
                break;
        }
    }
}
